package cn.wk.libs4a.view;

import java.util.List;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.Bitmap;
import android.net.Uri;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.CookieSyncManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import cn.wk.libs4a.R;
import cn.wk.libs4a.WKApplication;
import cn.wk.libs4a.WKBaseActivity;

/**
 * 公共Webview
 */
public class WKWebViewAct extends WKBaseActivity implements OnClickListener {

	private ImageView backButton, nextButton, refreshButton;
	WebView webView;
	String url = "";
	boolean isListUrlModel = false;
	String title_name = null;

	String cookieStr = ""; // cookie值

	@Override
	public void loadXml() {
		setContentView(R.layout.wk_act_web_weibo);
	} 

	@Override
	public void init() {
		WKHeader header = (WKHeader) findViewById(R.id.header);
		try {
			Intent in = getIntent();
			String type = in.getExtras().getString("isFrom");
			if (type.equals("weibo")) {
				url = in.getExtras().getString("url");
			} else {
				header.setTitle("详情");
				url = in.getExtras().getString("url");
			}
		} catch (NullPointerException e) {
			e.printStackTrace();
		}
		
		webView=(WebView)findViewById(R.id.content);
	}

	@Override
	public void setData() {
		try {
			CookieSyncManager.createInstance(this);
		} catch (Exception e) {
			e.printStackTrace();
		}

		try {
			webView.setFocusable(true);
			webView.requestFocus();
			webView.clearCache(true);
			webView.getSettings().setJavaScriptEnabled(true);
			webView.getSettings().setSupportZoom(true);
			webView.getSettings().setBuiltInZoomControls(true);
			WebSettings settings = webView.getSettings();
			settings.setUseWideViewPort(true);
			settings.setLoadWithOverviewMode(true);
			if (!url.equals("")) { // 链接访问：1、首页的特别推荐 2、。。。。
				webView.loadUrl(url);
				WebViewClient wc = new WebViewClient() {
					public void onPageStarted(WebView view, String url,
							Bitmap favicon) {
						super.onPageStarted(view, url, favicon);
						WKProgressDlg.showDialog(ctx, "");
					}

					public void onPageFinished(WebView view, String url) {
						super.onPageFinished(view, url);
						WKProgressDlg.stopDialog();
						refreshButtons();
					}

					public void doUpdateVisitedHistory(WebView view,
							String url, boolean isReload) {
						super.doUpdateVisitedHistory(view, url, isReload);
					}

					public boolean shouldOverrideUrlLoading(WebView view,
							String url) {
						return super.shouldOverrideUrlLoading(view, url);
					}
				};
				webView.setWebViewClient(wc);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 去商城打分
	 */
	private void goAppStore(String packageName) {
		Intent intentM = new Intent(Intent.ACTION_VIEW);
		intentM.setData(Uri.parse("market://details?id=" + packageName));
		intentM.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		List<ResolveInfo> list = getPackageManager().queryIntentActivities(
				intentM, PackageManager.MATCH_DEFAULT_ONLY);
		if (list != null && list.size() > 0) {
			ctx.startActivity(intentM);
		} else {
			WKApplication.showMsg("您的手机上安装应用市场，请手动下载！");
		}
	}

	void findView() {
		webView = (WebView) findViewById(R.id.content);
		backButton = (ImageView) findViewById(R.id.imageButton_back);
		nextButton = (ImageView) findViewById(R.id.imageButton_next);
		refreshButton = (ImageView) findViewById(R.id.imageButton_refresh);
		backButton.setOnClickListener(this);
		nextButton.setOnClickListener(this);
		refreshButton.setOnClickListener(this);
	}

	/**
	 * 更新按钮状态
	 */
	public void refreshButtons() {
//		if (webView.canGoBack()) {
//			// backButton.setImageResource(R.drawable.pop_up_arrow_down_hover);
//			backButton.setClickable(true);
//		} else {
//			// backButton.setImageResource(R.drawable.pop_up_arrow_down);
//			backButton.setClickable(false);
//		}
//		if (webView.canGoForward()) {
//			// nextButton.setImageResource(R.drawable.pop_up_arrow_up_hover);
//			nextButton.setClickable(true);
//		} else {
//			// nextButton.setImageResource(R.drawable.pop_up_arrow_up);
//			nextButton.setClickable(false);
//		}
	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		if (id == R.id.imageButton_back) {
			webView.goBack();
		} else if (id == R.id.imageButton_next) {
			webView.goForward();
		} else if (id == R.id.imageButton_refresh) {
			webView.reload();
		} else {
		}
	}
}
